﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DQYT.HTDZ.BLL;
using System.Data;
using DQYT.HTDZ.Model;

namespace DQYT.HTDZ.Web.Views.SystemManage
{
    public partial class MenuManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            #region

            lblMsg.Text = "";
            try
            {
                BDMenuBLL menuBLL = new BDMenuBLL();
                lblPath.Text = menuBLL.GetTitlePath(Request["mnu_id"]);
                if (!IsPostBack)
                {
                    btnEdit.Enabled = false;
                    btnDel.Enabled = false;
                    btnAddZMenu.Enabled = false;

                    Hidden1.Value = "0";
                    TreeBind();
                }
            }
            catch (Exception ex)
            {
                lblMsg.Text = "错误信息:" + ex.Message;
            }

            #endregion
        }

        private void TreeBind()
        {
            #region
            tvMenu.Nodes.Clear();
            DataTable dt = new DataTable();
            BDMenuBLL dMenu = new BDMenuBLL();
            dt = dMenu.GetList("").Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                //添加第一级
                string strPID = dt.Rows[i]["mnu_fid"].ToString();

                if (strPID == "0" || strPID == "")
                {
                    string strID = dt.Rows[i]["mnu_id"].ToString();
                    TreeNode tn = new TreeNode(dt.Rows[i]["mnu_mc"].ToString(), strID);
                    this.tvMenu.Nodes.Add(tn);
                    for (int j = 0; j < dt.Rows.Count; j++)
                    {
                        //添加第二级
                        string strPID1 = dt.Rows[j]["mnu_fid"].ToString();
                        string strID1 = dt.Rows[j]["mnu_id"].ToString();
                        if (strPID1 == strID)
                        {
                            TreeNode tn1 = new TreeNode(dt.Rows[j]["mnu_mc"].ToString(), strID1);
                            tn.ChildNodes.Add(tn1);
                            //添加第三级
                            for (int x = 0; x < dt.Rows.Count; x++)
                            {
                                string strPID3 = dt.Rows[x]["mnu_fid"].ToString();
                                string strID3 = dt.Rows[x]["mnu_id"].ToString();
                                if (strPID3 == strID1)
                                {
                                    TreeNode tn3 = new TreeNode(dt.Rows[x]["mnu_mc"].ToString(), strID3);
                                    tn1.ChildNodes.Add(tn3);
                                }
                            }
                        }
                    }
                }
            }
            #endregion
        }

        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {
            #region
            Hidden1.Value = tvMenu.SelectedValue;
            if (tvMenu.SelectedNode.Parent != null)
                Hidden2.Value = tvMenu.SelectedNode.Parent.Value;
            else
                Hidden2.Value = "0";

            BDMenuModel mMenu = new BDMenuModel();
            BDMenuBLL dMenu = new BDMenuBLL();
            mMenu = dMenu.GetModel(Convert.ToInt32(Hidden1.Value.ToString()));
            this.ViewState["menu"] = mMenu;
            this.txtMEN_MC.Text = mMenu.MNU_MC;
            this.txtMEN_URL.Text = mMenu.MNU_URL;
            this.txtMEN_MS.Text = mMenu.MNU_MS;
            this.txtMEN_BZ.Text = mMenu.MNU_BZ;
            TreeNode tnP = tvMenu.SelectedNode.Parent;
            btnDel.Enabled = true;
            btnAddZMenu.Enabled = true;
            btnEdit.Enabled = true;
            if (tnP != null)
            {
                TreeNode tnP2 = tnP.Parent;
                if (tnP2 != null)
                {
                    btnAddZMenu.Enabled = false;
                }
            }
            if (tvMenu.SelectedNode.ChildNodes.Count > 0)
            {
                btnDel.Enabled = false;
            }
            this.rbtnlMNU_ZT.SelectedValue = mMenu.MNU_ZT.ToString();

            #endregion
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            #region
            try
            {
                lblMsg0.Text = "";
                BDMenuBLL dMenu = new BDMenuBLL();
                bool isOK = dMenu.Delete(Convert.ToInt32(Hidden1.Value.ToString()));

                this.txtMEN_MC.Text = "";
                this.txtMEN_URL.Text = "";
                this.txtMEN_MS.Text = "";
                this.txtMEN_BZ.Text = "";

                TreeBind();
                btnDel.Enabled = false;
                lblMsg0.Text = "成功删除";
            }
            catch (Exception ex)
            {
                lblMsg0.Text = "错误信息:" + ex.Message;
            }
            #endregion
        }
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            #region
            try
            {
                lblMsg0.Text = "";
                BDMenuModel mMenu = new BDMenuModel();
                BDMenuBLL dMenu = new BDMenuBLL();
                mMenu = (BDMenuModel)this.ViewState["menu"];
                //首先判断名称是否为空
                mMenu.MNU_MC = this.txtMEN_MC.Text.Trim();
                mMenu.MNU_URL = this.txtMEN_URL.Text.Trim();
                if (mMenu.MNU_MC == "")
                {
                    lblMsg0.Text = "名称不能为空！";
                    return;
                }

                //然后判断是否重复
                int iNum = dMenu.GetNum(" MNU_ID<> " + Hidden1.Value + " AND MNU_MC='" + mMenu.MNU_MC + "'");
                if (iNum > 0)
                {
                    lblMsg0.Text = "菜单名称已经存在！";
                    return;
                }

                mMenu.MNU_BZ = txtMEN_BZ.Text.Trim();
                //mMenu.MNU_FID = Convert.ToInt32(Hidden1.Value);
                //mMenu.MNU_ID = dMenu.GetNextID();
                mMenu.MNU_MS = txtMEN_MS.Text.Trim();
                mMenu.MNU_SX = 0;
                mMenu.MNU_ZT = Convert.ToInt32(rbtnlMNU_ZT.SelectedValue);
                bool isOK = dMenu.Update(mMenu);
                if (isOK)
                {
                    lblMsg0.Text = "保存成功！";
                    txtMEN_BZ.Text = "";
                    txtMEN_MC.Text = "";
                    txtMEN_MS.Text = "";
                    txtMEN_URL.Text = "";
                    rbtnlMNU_ZT.SelectedValue = "1";
                    TreeBind();
                }
                else
                {
                    lblMsg0.Text = "保存失败！";
                }
            }
            catch (Exception ex)
            {
                lblMsg0.Text = "错误信息:" + ex.Message;
            }
            #endregion
        }
        protected void btnAddZMenu_Click(object sender, EventArgs e)
        {
            #region
            try
            {
                lblMsg0.Text = "";
                BDMenuModel mMenu = new BDMenuModel();
                BDMenuBLL dMenu = new BDMenuBLL();
                //首先判断名称是否为空
                mMenu.MNU_MC = this.txtMEN_MC.Text.Trim();
                mMenu.MNU_URL = this.txtMEN_URL.Text.Trim();
                if (mMenu.MNU_MC == "")
                {
                    lblMsg0.Text = "名称不能为空！";
                    return;
                }

                //然后判断是否重复
                int iNum = dMenu.GetNum("MNU_MC='" + mMenu.MNU_MC + "'");
                if (iNum > 0)
                {
                    lblMsg0.Text = "菜单名已经存在！";
                    return;
                }
                mMenu.MNU_BZ = txtMEN_BZ.Text.Trim();
                mMenu.MNU_FID = Convert.ToInt32(Hidden1.Value);
                mMenu.MNU_ID = dMenu.GetNextID();
                mMenu.MNU_MS = txtMEN_MS.Text.Trim();
                mMenu.MNU_SX = 0;
                mMenu.MNU_ZT = Convert.ToInt32(rbtnlMNU_ZT.SelectedValue);
                bool isOK = dMenu.Add(mMenu);
                if (isOK)
                {
                    lblMsg0.Text = "保存成功！";
                    txtMEN_BZ.Text = "";
                    txtMEN_MC.Text = "";
                    txtMEN_MS.Text = "";
                    txtMEN_URL.Text = "";
                    rbtnlMNU_ZT.SelectedValue = "1";
                    TreeBind();
                }
                else
                {
                    lblMsg0.Text = "保存失败！";
                }
            }
            catch (Exception ex)
            {
                lblMsg0.Text = "错误信息:" + ex.Message;
            }
            #endregion
        }
        protected void btnAddMenu_Click(object sender, EventArgs e)
        {
            #region
            try
            {
                lblMsg0.Text = "";
                BDMenuModel mMenu = new BDMenuModel();
                BDMenuBLL dMenu = new BDMenuBLL();
                //首先判断名称是否为空
                mMenu.MNU_MC = this.txtMEN_MC.Text.Trim();
                mMenu.MNU_URL = this.txtMEN_URL.Text.Trim();
                if (mMenu.MNU_MC == "")
                {
                    lblMsg0.Text = "名称不能为空！";
                    return;
                }

                //然后判断是否重复
                int iNum = dMenu.GetNum("MNU_MC='" + mMenu.MNU_MC + "'");
                if (iNum > 0)
                {
                    lblMsg0.Text = "菜单名已经存在！";
                    return;
                }
                mMenu.MNU_BZ = txtMEN_BZ.Text.Trim();
                
                if (tvMenu.SelectedNode == null)
                {
                    mMenu.MNU_FID = 0;
                }
                else
                {
                    mMenu.MNU_FID = Convert.ToInt32(tvMenu.SelectedNode.Parent.Value);
                }
                
                mMenu.MNU_ID = dMenu.GetNextID();
                mMenu.MNU_MS = txtMEN_MS.Text.Trim();
                mMenu.MNU_SX = 0;
                mMenu.MNU_ZT = Convert.ToInt32(rbtnlMNU_ZT.SelectedValue);
                bool isOK = dMenu.Add(mMenu);
                if (isOK)
                {
                    lblMsg0.Text = "保存成功！";
                    txtMEN_BZ.Text = "";
                    txtMEN_MC.Text = "";
                    txtMEN_MS.Text = "";
                    txtMEN_URL.Text = "";
                    rbtnlMNU_ZT.SelectedValue = "1";
                    btnDel.Enabled = false;
                    TreeBind();
                }
                else
                {
                    lblMsg0.Text = "保存失败！";
                }
            }
            catch (Exception ex)
            {
                lblMsg0.Text = "错误信息:" + ex.Message;
            }
            #endregion
        }
    }
}